import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/widgets/status_view.dart';

class PurchaseInWidget extends StatefulWidget {
  String? htmlText;

  PurchaseInWidget({Key? key, this.htmlText}) : super(key: key);

  @override
  _PurchaseInWidgetState createState() => _PurchaseInWidgetState();
}

class _PurchaseInWidgetState extends State<PurchaseInWidget> {
  ConfigNameDetail? article;
  late int status = LoadStatusType.loading;

  @override
  void initState() {
    super.initState();
    if (ObjectUtil.isEmpty(widget.htmlText)) {
      getConfigNameDetail("Purchase_Notes");
    } else {
      setState(() {
        status = LoadStatusType.success;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (status != LoadStatusType.success) {
      return StatusViews(status);
    }
    return Html(
        data: ObjectUtil.isEmpty(widget.htmlText)
            ? article?.value
            : widget.htmlText);
  }

  void getConfigNameDetail(String name) async {
    try {
      List list = await ServiceRepository.getConfigNameDetail(name);
      list.forEach((element) {
        if (element.name.toString().trim() == name) {
          article = element;
          status = LoadStatusType.success;
        } else {
          status = LoadStatusType.empty;
        }
      });
    } catch (e, s) {
      status = LoadStatusType.empty;
    }
    setState(() {});
  }
}
